using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Sucrose.Base
{
    public abstract class DownloadableItem : CourseItem
    {
        #region instanceVars

        private String remoteLocation;
        private String filename;
        private String localLocation;

        #endregion

        #region basicProperties

        public String RemoteLocation
        {
            get
            {
                return remoteLocation;
            }
            set
            {
                this.remoteLocation = value;
                this.filename = Util.GetFilenameFromPath(this.remoteLocation);
            }
        }

        public String Filename
        {
            get
            {
                return filename;
            }
            set
            {
                this.filename = value;
            }
        }

        public String LocalLocation
        {
            get
            {
                return localLocation;
            }
            set
            {
                this.localLocation = value;
            }
        }

        #endregion

        #region constructors

        /// <summary>
        /// Creates a new DownloadableItem.
        /// </summary>
        public DownloadableItem()
            : base()
        {
        }

        /// <summary>
        /// Creates a new DownloadableItem.
        /// </summary>
        /// <param name="id">ID of the DownloadableItem</param>
        /// <param name="name">Name of the DownloadableItem</param>
        /// <param name="description">A description of the DownloadableItem</param>
        /// <param name="url">The LMS page for the DownloadableItem</param>
        /// <param name="lastModified">Rhe last modified date of the DownloadableItem</param>
        /// <param name="course">The Course that this DownloadableItem is a part of</param>
        /// <param name="remoteLocation"></param>
        public DownloadableItem(int id, String name, String description, String url, DateTime lastModified, Course course, String remoteLocation) 
            : base(id, name, description, url, lastModified, course)
        {
            this.remoteLocation = remoteLocation;
            if(remoteLocation != null)
                this.filename = Util.GetFilenameFromPath(remoteLocation);
        }

        #endregion

        #region methods

        /// <summary>
        /// Downloads the Downloadable item to the specified path
        /// </summary>
        /// <param name="localPath">Path to download to (WITH TRAILING SLASH)</param>
        /// <returns>True on success, false on failure.</returns>
        protected void Download(String localPath)
        {
            if (!String.IsNullOrEmpty(remoteLocation))
            {
                LMSInterop lms = LMSInterop.GetInstance();
                string fullLoc = localPath + filename;

                this.localLocation = fullLoc;

                if ((File.Exists(fullLoc) && File.GetLastWriteTime(fullLoc) < this.LastModified) || !File.Exists(fullLoc))
                {
                    lms.DownloadDocument(remoteLocation, localLocation);
                }
            }
        }

        public abstract void Download();

        #endregion
    }
}